package pers.lt.admin.services;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pers.lt.admin.dao.DffcodeDaoImp;
import pers.lt.hibernatebeans.DffcodeInfoEntity;
import pers.lt.tools.StringFacs;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 18621 on 2016/7/5.
 */
@Component("DffcodeServicesImp")
@Transactional(readOnly=false)
public class DffcodeServicesImp {
    @Resource
    private DffcodeDaoImp dffcodeDaoImp;

    public DffcodeInfoEntity addDffcode(DffcodeInfoEntity dffcodeInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(dffcodeInfoEntity.getId())) dffcodeInfoEntity.setId(StringFacs.getGuid32());
        DffcodeInfoEntity a = dffcodeDaoImp.addDffcode(dffcodeInfoEntity);
        return a;
    }
    public DffcodeInfoEntity selectDffcode(DffcodeInfoEntity dffcodeInfoEntity){
        DffcodeInfoEntity a = dffcodeDaoImp.selectDffcode(dffcodeInfoEntity);
        return a;
    }
    public List<Map> dffcodeList(DffcodeInfoEntity dffcodeInfoEntity){
        List <Map> rList = new ArrayList<Map>();
        List <Map> list = dffcodeDaoImp.dffcodeList(dffcodeInfoEntity);

        for(int i=0 ;i<list.size();i++){
            Map map = list.get(i);
            int childCount  = 0;
            if(!StringFacs.isEmpty(map.get("CHILD"))) childCount = Integer.parseInt(map.get("CHILD").toString());
            if(childCount>0){
                //Map mapT = new HashMap();
               // mapT.put("ID",map.get("ID")+"_1");
                //mapT.put("DFFCODE_NAME","Ttest");
               // List a = new ArrayList<Map>();
                //a.add(mapT);
                //map.put("children",a);
                map.put("state","closed");
            }//else map.put("children",new ArrayList<Map>());
            rList.add(map);
        }
        return rList;
    }
    //组织COMBOXTREE数据格式的树
    public List<Map> dffcodeListTree(DffcodeInfoEntity dffcodeInfoEntity){
        List <Map> rList = new ArrayList<Map>();
        List <Map> list = dffcodeDaoImp.dffcodeList(dffcodeInfoEntity);
        //要把 ‘list’ Map中的  ID 和 DFFCODE_NAME 两个字段改变成id ,text 因为easyui comomtree组件只认这个格式 结果放到rList中
        for(int i=0 ;i<list.size();i++){
            Map map = list.get(i);
            Map rMap = new HashMap();
            rMap.put("id",map.get("ID"));
            rMap.put("text",map.get("DFFCODE_NAME"));
            rMap.put("state","closed");
            rList.add(rMap);
        }
        return rList;
    }
    public void dffcodeDelete(DffcodeInfoEntity dffcodeInfoEntity){
        String [] ary= dffcodeInfoEntity.getId().split("~");
        for(String str:ary){
            DffcodeInfoEntity dffcodeInfoEntity1 = new DffcodeInfoEntity();
            dffcodeInfoEntity1.setId(str);
            if(!StringFacs.isEmpty(str))dffcodeDaoImp.dffcodeDelete(dffcodeDaoImp.selectDffcode(dffcodeInfoEntity1));
        }
    }
}
